package com.buddy.learn.javasource._thread;

import org.apache.commons.collections4.MapUtils;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Buddy
 * @date 2021/5/13
 */
public class _ThreadLocal {

    static final ThreadLocal<Map<String,String>> context = new ThreadLocal<>();

    @Test
    public void testThread(){
        Map<String,String> contextMap = context.get();
        if (MapUtils.isEmpty(contextMap)){
            contextMap = new HashMap<>(4);
        }
        contextMap.put("k1","v1");
        context.set(contextMap);
        System.out.println("k1,v1 被放到上下文中");
        getFromContext();
    }

    private String getFromContext() {
        String v1 = context.get().get("k1");
        System.out.println("v1");
        context.remove();
        return v1;
    }
}
